package com.lrd.snapshotorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrd.snapshotorder.entity.SenderAddress;
import com.lrd.snapshotorder.protocol.dto.address.SenderAddressQueryDto;

import java.util.Objects;

public interface SenderAddressMapper extends BaseMapper<SenderAddress> {

    default Page<SenderAddress> querySenderAddressList(SenderAddressQueryDto dto) {
        return this.selectPage(
                new Page<>(dto.getPageIndex(), dto.getPageSize()),
                Wrappers.lambdaQuery(SenderAddress.class)
                        .eq(StringUtils.isNotBlank(dto.getSender()), SenderAddress::getSender, dto.getSender())
                        .eq(StringUtils.isNotBlank(dto.getSenderAddress()), SenderAddress::getSenderAddress, dto.getSenderAddress())
                        .eq(Objects.nonNull(dto.getStatus()), SenderAddress::getStatus, dto.getStatus()));
    }

    default SenderAddress querySenderAddress() {
        return this.selectOne(
                Wrappers.lambdaQuery(SenderAddress.class)
                        .eq(SenderAddress::getStatus, 1)
                        .last("limit 1")
        );
    }
}